package cn.johnyu.neo4j;

import org.neo4j.driver.*;

import static org.neo4j.driver.Values.parameters;

public class Neo4jCRUDExample {
    private final Driver driver;

    public Neo4jCRUDExample(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }

    public void close() {
        driver.close();
    }

    public void createPerson(String name) {
        try (Session session = driver.session()) {
            session.writeTransaction(tx -> tx.run("CREATE (a:Person {name: $name})", parameters("name", name)));
            System.out.println("Created a person named " + name);
        }
    }

    public void deleteAll(){
        try (Session session = driver.session()) {
            session.writeTransaction(tx -> tx.run("MATCH (n) DETACH DELETE n"));
            System.out.println("删除全部。。。");
        }
    }

    public void createR(String name1,String name2){
        try (Session session = driver.session()) {
            session.writeTransaction(tx -> tx.run("MATCH (p1:Person {name: $name1}), (p2:Person {name: $name2}) CREATE (p1)-[:KNOWS]->(p2)"
            ,parameters("name1",name1,"name2",name2)));
            System.out.printf("建立%s 和 %s 之间的单向关系\n",name1,name2);
        }
    }

//    public void findPerson(String name) {
//        try (Session session = driver.session()) {
//            Result result = session.readTransaction(tx -> tx.run("MATCH (a:Person {name: $name}) RETURN a.name AS name", parameters("name", name)));
////            result.stream().map(record -> record.get("name").asString()).forEach(System.out::println);
//            result.list(record -> record.get("name").asString()).forEach(System.out::println);
//
//        }
//
//    }
    public void findPerson(String name) {
        try (Session session = driver.session()) {
            session.readTransaction(tx -> {
                Result result = tx.run("MATCH (a:Person {name: $name}) RETURN a.name AS name", parameters("name", name));
                result.stream().map(record -> record.get("name").asString()).forEach(System.out::println);
                return null; // 由于这里不需要返回结果，返回null
            });
        }
    }


    public void updatePerson(String name, String newName) {
        try (Session session = driver.session()) {
            session.writeTransaction(tx -> tx.run("MATCH (a:Person {name: $name}) SET a.name = $newName RETURN a.name AS name",
                    parameters("name", name, "newName", newName)));
            System.out.println("Updated person's name to " + newName);
        }
    }

    public void deletePerson(String name) {
        try (Session session = driver.session()) {
            session.writeTransaction(tx -> tx.run("MATCH (a:Person {name: $name}) DETACH  DELETE a", parameters("name", name)));
            System.out.println("Deleted person named " + name);
        }
    }

    public static void main(String... args) throws Exception {
        Neo4jCRUDExample example = new Neo4jCRUDExample("bolt://localhost:7687", "neo4j", "12345678");
//        example.deleteAll();
//
//        example.createPerson("Nina");
//        example.createPerson("Jack");
//        example.createPerson("Tom");
//        example.createPerson("John");
//        example.createPerson("Mike");
//
//        example.createR("Nina","Jack");
//        example.createR("Nina","Tom");
//        example.createR("Jack","Tom");
//        example.createR("Tom","John");
//        example.createR("Tom","Mike");

//        example.createPerson("John Doe");

//        example.findPerson("John");

//        example.updatePerson("John", "Johnson");

//        example.deletePerson("Johnson");
        example.close();

    }
}
